// 第一题
// 有下面两个数组
// [A1, A2, A3, A4, B1, B2, B3, B4, C1, C2, C3, D1]
// [A, B, C, D]
// 将他们合并为新的数组：[A1, A2, A3, A4, A, B1, B2, B3, B4, B, C1, C2, C3, C, D1, D]

var arr1 = ['A1', 'A2', 'A3', 'A4', 'B1', 'B2', 'B3', 'B4', 'C1', 'C2', 'C3', 'D1'];
var arr2 =  ['A', 'B', 'C', 'D'];
function combineArr(arr1, arr2) {
    let result = arr1;
    let currentIndex = 0;
    for(let i = 0; i< arr2.length; i++) {
        let reg = new RegExp(arr2[i]);
        while(currentIndex <= arr1.length - 1) {
            currentIndex ++;
            if (!reg.test(arr1[currentIndex])) {
                result.splice(currentIndex, 0, arr2[i]);
                break;
            };
        }
    }
    return result;
};

// console.log(combineArr(arr1, arr2));

function combineArr2(arr1, arr2) {
    let result = [];
    let currentIndex = 0;
    for(var i =0 ; i < arr1.length; i++) {
        if (arr1[i].charAt(0) === arr2[currentIndex]) {
            result.push(arr1[i]);
        } else {
            result.push(arr2[currentIndex]);
            currentIndex++;
            i--;
        };
        if (i === arr1.length - 1) {
            result.push(arr2[currentIndex])
        }
    }
    return result;
}
console.log(combineArr2(arr1, arr2));
